
package com.memorypalace.biz;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.memorypalace.knowledge.mapper.KnowledgeTreeMapper;
import com.memorypalace.knowledge.mapper.NoteConnectMapper;
import com.memorypalace.knowledge.pojo.KnowledgeTree;
import com.memorypalace.knowledge.pojo.Node;
import com.memorypalace.knowledge.pojo.NoteConnect;
import com.memorypalace.pojo.Note;

@Service("knowledgeTreeBiz")
public class KnowledgeTreeBiz implements IKnowledgeTreeBiz {

	@Autowired
	private KnowledgeTreeMapper knowledgeTreeMapper;

	@Autowired
	private NoteConnectMapper noteConnectMapper;

	/**
	 * 返回的必须是有顺序的nodes列表 只查找类型为节点的knowledgeTree
	 */
	@Override
	public List<Node> getNodesByUsersKnowledgeTree(String userId, String knowledgeId) {
		List<KnowledgeTree> knowledgeTrees = knowledgeTreeMapper.getKnowledgeTreeByUserId(userId);
		List<Node> nodes = new ArrayList<Node>();
		for (KnowledgeTree knowledgeTree : knowledgeTrees) {
			System.out.println(knowledgeTree);
			if (knowledgeId != null) {
				// 不要当前知识体系以及其下的
				if (knowledgeId.equals(knowledgeTree.getKnowledgeId())) {
					// 当前是要剔除 的知识体系
					continue;
				}
			}
			// 找到当前知识体系下的所有笔记
			knowledgeTree = knowledgeTreeMapper
					.getKnowledgeTreeByKnowledgeIdWithNoteList(knowledgeTree.getKnowledgeId());
			// 找到当前知识体系下的所有子知识体系
			List<KnowledgeTree> subKnowledgeTreeList = knowledgeTreeMapper
					.getKnowledgeTreeByFatherId(knowledgeTree.getKnowledgeId());
			Integer specialKnowledgeCount = 0;
			for (KnowledgeTree knowledgeTree2 : subKnowledgeTreeList) {
				if (knowledgeTree2.getKnowledgeType() != 1) {
					specialKnowledgeCount++;
				}
			}
			// if(knowledgeTree.getKnowledgeType() != 1){
			// // 只查找类型为节点的knowledgeTree
			// continue;
			// }
			Node node = new Node();
			if (knowledgeTree.getFatherKnowledgeId() == null) {
				node.setParentid("root");
			} else {
				node.setParentid(knowledgeTree.getFatherKnowledgeId());
			}
			node.setTopic(knowledgeTree.getKnowledgeName());
			node.setId(knowledgeTree.getKnowledgeId());
			node.setDirection("left");
			node.setExpanded(false);
			node.getData().put("type", knowledgeTree.getKnowledgeType());
			node.getData().put("notes",
					noteListToNodeList(knowledgeTree.getKnowledgeId(), knowledgeTree.getNoteList()));
			node.getData().put("specialKnowledgeTrees", specialKnowledgeCount);

			// parentId topic id
			nodes.add(node);
		}
		nodes = sortNodes(nodes);
		System.out.println("nodeList:" + new Gson().toJson(nodes));
		return nodes;
	}

	private List<Node> noteListToNodeList(String knowledgeTreeId, List<Note> noteList) {
		List<Node> nodes = new ArrayList<Node>();
		for (Note note : noteList) {
			Node node = new Node();
			node.setParentid(knowledgeTreeId);
			node.setTopic(note.getTitle());
			node.setId(knowledgeTreeId + ":" + note.getNoteId());
			node.getData().put("type", 0);
			nodes.add(node);
		}
		return nodes;
	}

	private List<Node> sortNodes(List<Node> nodes) {
		List<Node> temp = new ArrayList<Node>();
		List<Node> temp2 = new ArrayList<Node>();
		for (Node node : nodes) {
			temp2.add(node);
		}
		for (Node node : nodes) {
			if ("root".equals(node.getParentid())) {
				// 如果是根节点的子节点
				temp.add(node);
				temp2.remove(node);
				temp.addAll(getSubNodes(node.getId(), temp2));
			}
		}
		return temp;
	}

	private List<Node> getSubNodes(String fatherId, List<Node> nodes) {
		List<Node> temp = new ArrayList<Node>();
		List<Node> temp2 = new ArrayList<Node>();
		for (Node node : nodes) {
			temp2.add(node);
		}
		for (Node node : nodes) {
			if (fatherId.equals(node.getParentid())) {
				// 如果是根节点的子节点
				temp.add(node);
				temp2.remove(node);
				temp.addAll(getSubNodes(node.getId(), temp2));
			}
		}
		return temp;
	}

	@Override
	public Integer addKnowledgeTree(KnowledgeTree knowledgeTree) {
		return knowledgeTreeMapper.addKnowledgeTree(knowledgeTree);
	}

	@Override
	public KnowledgeTree getKnowledgeTree(String knowledgeId) {
		KnowledgeTree result = new KnowledgeTree();
		result = knowledgeTreeMapper.getKnowledgeTreeByKnowledgeId(knowledgeId);
		result.setNoteList(knowledgeTreeMapper.getKnowledgeTreeByKnowledgeIdWithNoteList(knowledgeId).getNoteList());

		return result;
	}

	@Override
	public Integer updateKnowledgeTree(KnowledgeTree knowledgeTree) {
		return knowledgeTreeMapper.modifyKnowledgeTree(knowledgeTree);
	}

	@Override
	public Integer deleteKnowledgeTree(String knowledgeId) {
		// 找到当前知识体系下的所有子知识体系
		List<KnowledgeTree> subKnowledgeTreeList = knowledgeTreeMapper
				.getKnowledgeTreeByFatherId(knowledgeId);
		for (KnowledgeTree subKnowledgeTree : subKnowledgeTreeList) {
			noteConnectMapper.deleteNoteConnectsByFatherKnowledgeId(subKnowledgeTree.getKnowledgeId());
			knowledgeTreeMapper.deleteKnowledgeTree(subKnowledgeTree.getKnowledgeId());
		}
		return knowledgeTreeMapper.deleteKnowledgeTree(knowledgeId);
	}

}
